package org.example.BinaryTree;

public class Minimum_absolute_difference {
    public static TreeNode min = null;
    public static int result = Integer.MAX_VALUE;
    public static int temp;
    public static void main(String[] args) {
        //二叉搜索树的最小绝对差

        //给你一个二叉搜索树的根节点 root ，返回 树中任意两不同节点值之间的最小差值 。
        //差值是一个正数，其数值等于两值之差的绝对值。
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(7);
        TreeNode node3 = new TreeNode(1);
        TreeNode node4 = new TreeNode(3);
        TreeNode root = new TreeNode(4);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        System.out.println(getMinimumDifference(root));
    }
    public static int getMinimumDifference(TreeNode root) {
        get(root);
        return result;
    }
    public static void get(TreeNode root){
        if (root==null) return;
        get(root.left);
        if (min!=null){
            temp  = root.val - min.val;
            if (temp<result) result = temp;
        }
        min = root;
        get(root.right);
    }
}
